<template>
    <rx-dialog style="height: 100%;" @handOk="onOk" @cancel="cancel">

        <a-form :label-col="{ span: 5 }" :wrapper-col="{ span: 12 }">
            <a-form-item :label="i18n('formReport.reportPrint','报表打印')">
                <rx-input-button
                    v-model="dialogVal"
                    @click="selectUreportView"
                    style='width: 300px;'
                >
                </rx-input-button>
            </a-form-item>
            <a-form-item :label="i18n('list.paramConfig','参数配置')">
                <a-table :dataSource="config.params" :pagination="false"
                         rowKey="idx_">
                    <a-table-column :title="i18n('formReport.reportSelParam','报表参数')" dataIndex="bindFieldLabel" >
                        <template slot-scope="text, record">
                            <a-form-item class="tableItems">
                                {{ record.bindFieldLabel }}
                            </a-form-item>
                        </template>
                    </a-table-column>
                    <a-table-column :title="$comi18('formDef.valueSource', '值来源')" dataIndex="valueSource"  >
                        <template slot-scope="text, record">
                            <a-select v-model="record.valueSource"
                                      :options="valueSourceDef"
                                      :getPopupContainer="(p)=>p.parentNode"
                                      :allowClear="true"
                            >
                            </a-select>
                        </template>
                    </a-table-column>
                    <a-table-column :title="$comi18('rxDialog.dbObj.paramVal', '参数值')" dataIndex="returnField" >
                        <template slot-scope="text, record">
                            <a-form-item >
                                <a-select style="width: 200px;"
                                    v-if="record.valueSource == 'constantVar'"
                                    v-model="record.returnValue"
                                    :defaultValue="record.returnValue"
                                    :options="constantField"
                                    allowClear
                                    :getPopupContainer="(p) => p.parentNode"
                                ></a-select>
                                <a-select style="width: 200px;"
                                          v-if="record.valueSource == 'param'"
                                          v-model="record.returnValue"
                                          :defaultValue="record.returnValue"
                                          :options="headerFields"
                                          :getPopupContainer="p=>p.parentNode" allowClear>
                                </a-select>
                                <a-input style="width: 200px;" v-model="record.returnValue"
                                         v-if="record.valueSource == 'fixedVar'"></a-input>
                            </a-form-item>

                        </template>
                    </a-table-column>
                </a-table>
            </a-form-item>
        </a-form>
    </rx-dialog>

</template>

<script>
import {RxDialog, Util} from "@lowcode/jpaas-common-lib";
import FormBoListApi from "@/api/form/core/formBoList";
import FormSolutionApi from '@/api/form/core/formSolution'
import UreportListDialog from "@/views/modules/form/core/UreportListDialog";
import ComponentDef from "@/views/modules/form/core/formComponent/ComponentDef";
import FormCustomQueryApi from "@/api/form/core/formCustomQuery";
import PublicApi from "@/api/form/core/public";

export default {
    name: "FormSolutionOpenUreportDialog",
    mixins: [ComponentDef],
    props: {
        layerid: {type: String},
        lydata: {type: Object},
        destroy: Function,
        //列表主键
        pkId: {type: String},
        //配置信息
        customConfig: {
            type: Object
        },

    },
    data() {
        return {
            boEntityShow: false,
            dialogVal: "",
            config: {
                alias: "",//alias
                name: "",//名称
                params:[],
                callBack:"",
                //窗口大小
                isMax:"YES",
                height:600,
                width:800
            },
            headerFields:[],
            constantField: [],
            valueSourceDef: [
                {value: 'param', label: this.$ti18('rxForm.param', '参数传入')},
                {value: 'fixedVar', label: this.$ti18('rxForm.fixedVar', '固定值')},
                {value: 'constantVar', label: this.$ti18('rxForm.constantVar', '常量')},
            ],
            isReportExist:false,
        }
    },
    created() {
        this.init();
    },
    methods: {
        i18n(name,text,key){
            return this.$ti18(name,text,"formBoList",key);
        },
        init(){
            this.config=this.customConfig;
            console.log(this.config.alias)
            this.getColumns();
            this.dialogVal = JSON.stringify({text: this.config.name, value: this.config.name});
            this.loadConstants();
        },
        onOk() {
            Util.closeWindow(this, "ok", this.config);
        },
        cancel() {
            Util.closeWindow(this, "cancel");
        },
        getColumns() {
            let self_ = this;
            FormSolutionApi.getColumnsbyAlias(this.config.alias).then(res=>{
                self_.headerFields =res.data.map(function (item) {
                    console.log(item.FIELD_NAME_)
                    item.value=item.FIELD_NAME_;
                    item.label=item.COMMENT_;
                    return item
                });
                self_.$forceUpdate();
            })
        },
        loadConstants() {
            FormCustomQueryApi.getConstants().then((data) => {
                this.constantField=[]
                this.constantField = this.constantField.concat(
                    data.map((item) => {
                        return { label: item.val, value: item.key }
                    })
                )
            })
        },
        selectUreportView(){
            var self = this;
            var config = {
                curVm: this,
                "data": {
                    "allowEdit": false
                },
                component: UreportListDialog,
                title: this.i18n('formReport.reportPrint','报表打印'),
                widthHeight: ['800px', '600px']
            };
            Util.open(config, function (action, data) {
                if (action != 'ok') return;
                //先清空数据再添加
                self.config.params=[];
                var obj=data[0];
                self.dialogVal = JSON.stringify({text: obj.name, value: obj.alias});
                //self.config.alias=obj.alias;
                self.config.name=obj.name;
                if(obj.ureportFileQueryParameterList){
                    //var params=JSON.parse(data.params);
                    var params=obj.ureportFileQueryParameterList
                    for (let i = 0; i < params.length; i++) {
                        self.config.params.forEach(item =>{
                            if(item.bindFieldLabel == params[i].name){
                                this.$message.warning(self.i18n('formReport.reportExist','该报表已存在'));
                                this.isReportExist = true;
                            }
                        })
                        //如果报表已存在
                        if(this.isReportExist){
                            return;
                        }
                        self.config.params.push({
                            bindFieldLabel:params[i].name,
                            returnLabel:"",
                            returnValue:""
                        })
                    }
                }else {
                    self.config.params=[];
                }
                self.customConfig=self.config;
            });
        }
    },

}
</script>

<style scoped>

</style>
